{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "explicit-diamond",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[32mimport \u001b[39m\u001b[36mscala.math._\n",
       "\n",
       "\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mCircle\u001b[39m\r\n",
       "defined \u001b[32mobject\u001b[39m \u001b[36mCircle\u001b[39m\r\n",
       "\u001b[36mcircle1\u001b[39m: \u001b[32mCircle\u001b[39m = \u001b[33mCircle\u001b[39m(\u001b[32m5.0\u001b[39m)\r\n",
       "\u001b[36mres2_4\u001b[39m: \u001b[32mDouble\u001b[39m = \u001b[32m78.53981633974483\u001b[39m"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "//伴生对象\n",
    "import scala.math._\n",
    "\n",
    "case class Circle(radius:Double){\n",
    "    import Circle._\n",
    "    def area :Double = calculateArea(radius)\n",
    "}\n",
    "\n",
    "object Circle {\n",
    "    private def calculateArea(radius:Double):Double = Pi * pow(radius,2)\n",
    "}\n",
    "\n",
    "val circle1 = Circle(5)\n",
    "\n",
    "circle1.area"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "mature-piece",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   ***\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mEmail\u001b[39m\r\n",
       "defined \u001b[32mobject\u001b[39m \u001b[36mEmail\u001b[39m\r\n",
       "\u001b[36mscalaCenterEmail\u001b[39m: \u001b[32mOption\u001b[39m[\u001b[32mEmail\u001b[39m] = \u001b[32mNone\u001b[39m"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Email (val username:String,val domainName:String)\n",
    "\n",
    "object Email {\n",
    "    def fromString(emailString:String):Option[Email] ={\n",
    "        emailString.split(\"@\") match {\n",
    "            case Array(a,b) => Some(new Email(a,b))\n",
    "            case _ => None\n",
    "        } \n",
    "    }\n",
    "    \n",
    "}\n",
    "\n",
    "val scalaCenterEmail = Email.fromString(\"scala.centerxxx.ch\")\n",
    "scalaCenterEmail match {\n",
    "    case Some(email) => println(\n",
    "    s\"\"\"Registered an email\n",
    "        |xxx:${email.username}\n",
    "        |yyy:${email.domainName}\n",
    "     \"\"\"\n",
    "    )\n",
    "    case None => println(\"   ***\")\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "sacred-miller",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
